<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <div class="form-div">
      <div class="form-line">
        <a-input v-model:value="queryParam.shortName" id="shareId0" />
        <BlockOutlined @click="copy(0)" />
      </div>
      <div class="form-line">
        <a-input v-model:value="queryParam.longName" id="shareId1" />
        <BlockOutlined @click="copy(1)" />
      </div>
      <div class="form-line">
        <a-input v-model:value="queryParam.vermenovelaUrl" id="shareId2" />
        <BlockOutlined @click="copy(2)" />
      </div>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick, defineExpose } from 'vue';
  import BookIndexForm from './BookIndexForm.vue';
  import { message } from 'ant-design-vue';
  import { BlockOutlined } from '@ant-design/icons-vue';
  const title = ref<string>('');
  const width = ref<number>(800);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);

  const queryParam = reactive({
    shortName: '',
    longName: '',
    vermenovelaUrl: '',
  });

  function copy(id) {
    let content = document.getElementById('shareId' + id);
    if (content.value) {
      content.select();
      document.execCommand('copy');
      window.getSelection().removeAllRanges();
    }
    message.success('复制成功');
  }

  /**
   * 新增
   */
  function add(record) {
    title.value = '分享';
    visible.value = true;
    console.log(record, 555666);
    queryParam.shortName = record.shareUrl[0];
    queryParam.longName = record.shareUrl[1];
    queryParam.vermenovelaUrl = record.shareUrl[2] || '';
  }

  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    title.value = disableSubmit.value ? '详情' : '编辑';
    visible.value = true;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }

  .form-div {
    padding: 0 30px 30px 30px;
    .form-line {
      display: flex;
      align-items: center;
      margin-top: 30px;
      .anticon-block {
        font-size: 16px;
        margin-left: 12px;
        cursor: pointer;
      }
    }
  }
</style>
